package com.fiberhome.ztelauncher.core.page;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.fiberhome.ztelauncher.R;
import com.fiberhome.ztelauncher.uilib.AutoViewGroup;
import com.fiberhome.ztelauncher.xml.bean.ViewBean;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class BaseFragment extends Fragment {


    private List<ViewBean> viewBeanList = new ArrayList<ViewBean>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base, container, false);
        AutoViewGroup autoview = (AutoViewGroup) view.findViewById(R.id.autoview);
        for (int i = 0; i < 15; i++) {
            ViewBean viewBean = new ViewBean();
//            setData(viewBean, i);
            setDataHorizontal(viewBean, i);
            viewBeanList.add(viewBean);
        }
//        autoview.setGridCount(5, 12);
        autoview.setGridCount(10, 5);
        autoview.setDivide(20, Color.parseColor("#ffffff"));
        autoview.setItemViewShape(R.drawable.selector_background);
//        autoview.setScrollDirection(AutoViewGroup.SCROLL_VERTICAL);

        autoview.addItemViews(viewBeanList, new AutoViewGroup.OnAddLayoutListener() {
            @Override
            public View setLayout(int width, int height, final int position) {
                View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.item, null);
                TextView text = itemView.findViewById(R.id.tv_count);
                text.setLayoutParams(new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.WRAP_CONTENT));
                text.setText("第" + (position + 1) + "个布局");
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });
                return itemView;
            }
        });
        return view;
    }

    private void setData(ViewBean viewBean, int i) {
        if (i == 0) {
            viewBean.left = 0;
            viewBean.top = 0;
            viewBean.width = 1;
            viewBean.height = 1;
        } else if (i == 1) {
            viewBean.left = 1;
            viewBean.top = 0;
            viewBean.width = 2;
            viewBean.height = 2;
        } else if (i == 2) {
            viewBean.left = 3;
            viewBean.top = 0;
            viewBean.width = 2;
            viewBean.height = 1;
        } else if (i == 3) {
            viewBean.left = 0;
            viewBean.top = 1;
            viewBean.width = 1;
            viewBean.height = 2;
        } else if (i == 4) {
            viewBean.left = 1;
            viewBean.top = 2;
            viewBean.width = 1;
            viewBean.height = 2;
        } else if (i == 5) {
            viewBean.left = 2;
            viewBean.top = 2;
            viewBean.width = 1;
            viewBean.height = 2;
        } else if (i == 6) {
            viewBean.left = 3;
            viewBean.top = 1;
            viewBean.width = 2;
            viewBean.height = 1;
        } else if (i == 7) {
            viewBean.left = 0;
            viewBean.top = 3;
            viewBean.width = 1;
            viewBean.height = 1;
        } else if (i == 8) {
            viewBean.left = 3;
            viewBean.top = 2;
            viewBean.width = 2;
            viewBean.height = 1;
        } else if (i == 9) {
            viewBean.left = 0;
            viewBean.top = 4;
            viewBean.width = 3;
            viewBean.height = 1;
        } else if (i == 10) {
            viewBean.left = 3;
            viewBean.top = 3;
            viewBean.width = 2;
            viewBean.height = 3;
        } else if (i == 11) {
            viewBean.left = 0;
            viewBean.top = 5;
            viewBean.width = 3;
            viewBean.height = 1;
        } else if (i == 12) {
            viewBean.left = 0;
            viewBean.top = 6;
            viewBean.width = 5;
            viewBean.height = 2;
        } else if (i == 13) {
            viewBean.left = 0;
            viewBean.top = 8;
            viewBean.width = 5;
            viewBean.height = 2;
        } else if (i == 14) {
            viewBean.left = 0;
            viewBean.top = 10;
            viewBean.width = 5;
            viewBean.height = 2;
        }
    }

    private void setDataHorizontal(ViewBean viewBean, int i) {
        if (i == 0) {
            viewBean.left = 0;
            viewBean.top = 0;
            viewBean.width = 2;
            viewBean.height = 2;
        } else if (i == 1) {
            viewBean.left = 2;
            viewBean.top = 0;
            viewBean.width = 2;
            viewBean.height = 1;
        } else if (i == 2) {
            viewBean.left = 4;
            viewBean.top = 0;
            viewBean.width = 2;
            viewBean.height = 2;
        } else if (i == 3) {
            viewBean.left = 6;
            viewBean.top = 0;
            viewBean.width = 2;
            viewBean.height = 2;
        } else if (i == 4) {
            viewBean.left = 8;
            viewBean.top = 0;
            viewBean.width = 2;
            viewBean.height = 3;
        } else if (i == 5) {
            viewBean.left = 0;
            viewBean.top = 2;
            viewBean.width = 2;
            viewBean.height = 2;
        } else if (i == 6) {
            viewBean.left = 2;
            viewBean.top = 1;
            viewBean.width = 2;
            viewBean.height = 2;
        } else if (i == 7) {
            viewBean.left = 4;
            viewBean.top = 2;
            viewBean.width = 2;
            viewBean.height = 2;
        } else if (i == 8) {
            viewBean.left = 6;
            viewBean.top = 2;
            viewBean.width = 2;
            viewBean.height = 3;
        } else if (i == 9) {
            viewBean.left = 8;
            viewBean.top = 3;
            viewBean.width = 2;
            viewBean.height = 2;
        } else if (i == 10) {
            viewBean.left = 0;
            viewBean.top = 4;
            viewBean.width = 2;
            viewBean.height = 1;
        } else if (i == 11) {
            viewBean.left = 2;
            viewBean.top = 3;
            viewBean.width = 2;
            viewBean.height = 2;
        } else if (i == 12) {
            viewBean.left = 4;
            viewBean.top = 4;
            viewBean.width = 2;
            viewBean.height = 1;
        }
    }

}
